package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.CouponRecord;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.util.XtomTimeUtil;

public class CouponsRecordAdapter extends BaseAdapter {

    private ArrayList<CouponRecord> coupons;
    private int type = -1;//显示类型：0获取记录，1消费记录

    public CouponsRecordAdapter(Context mContext, ArrayList<CouponRecord> coupons, int type) {
        super(mContext);
        this.coupons = coupons;
        this.type = type;
    }

    @Override
    public int getCount() {
        int size = coupons == null ? 0 : coupons.size();
        return size == 0 ? 1 : coupons.size();
    }

    @Override
    public boolean isEmpty() {
        int size = coupons == null ? 0 : coupons.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.listitem_coupon, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        }
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_mark)
        TextView tvMark;
        @Bind(R.id.tv_quota)
        TextView tvQuota;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.tv_date)
        TextView tvDate;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setDatas(int position) {
            CouponRecord record = coupons.get(position);
//            tvName.setText(record.getName());
//            if (!isNull(record.getUsername()))
//                tvName.append("："+record.getUsername());
            tvQuota.setText(uuUtils.formatAfterDot2(record.getScore()));
            tvDate.setText(XtomTimeUtil.TransTime(record.getRegdate(), "yyyy-MM-dd"));
            if (type == 0) {//0获取记录
                tvMark.setText("+");
                ImageUtils.loadAvatar(mContext, record.getAvatar(), ivAvatar);
                //获取类型	1：系统赠送;2：转赠;3：受赠;4：购买商品;5:退款所得;7:充值记录
                if (record.getKeytype().equals("1")) {
                    tvName.setText("系统赠送");
                } else if (record.getKeytype().equals("2")) {
                    tvName.setText("转赠");
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("3")) {
                    tvName.setText("受赠");
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("4") || record.getKeytype().equals("6")) {
                    tvName.setText(record.getName());
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("5")) {
                    tvName.setText("退款所得");
                    if (!isNull(record.getName()))
                        tvName.append(" "+record.getName());
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("7")) {
                    tvName.setText("充值");
                }
            } else if (type == 1) {
                ivAvatar.setVisibility(View.GONE);
                tvMark.setText("-");
                //消费类型  1：消费;2：转赠;3：代付;4：线下消费
                if (record.getKeytype().equals("1") || record.getKeytype().equals("4")) {
                    tvName.setText(record.getName());
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("2")) {
                    tvName.setText("转赠");
                    if (!isNull(record.getUsername()))
                        tvName.append("："+record.getUsername());
                } else if (record.getKeytype().equals("3")) {
                    tvName.setText("代付:" + record.getUsername());
                } else if (record.getKeytype().equals("4")) {
                    tvName.setText("线下消费");
                }
            }
            if (!isNull(record.getBill_sn())) {
                tvContent.setText("订单号：" + record.getBill_sn());
            }else
                tvContent.setText("");
        }
    }
}
